﻿using System;
using System.Collections.Generic;
using DragonsEyeAdventure.Utilities;

/*
 * Author: Nathaniel Edgar
 * Contributors:
 * File Version: v0.4
 * 
 * Copyright (C) 2013 Nathaniel Edgar
 * For full copyright information, please see the project README
 */

namespace DragonsEyeAdventure
{
    /// <summary>
    /// Main class containing master controls and main method
    /// </summary>
    class Program
    {
        #region Variables
        //Basic console settings
        public const string consoleTitle = "The Dragon's Eye: A Text Adventure";
        public const int width = 80;
        public const int height = 30;
        public const string version = "v0.1pa";
        //Tracker variables
        private static bool running = true;
        private static List<string> errors = new List<string>();    //Used to track errors; later will be output to log file
        private static bool mainMenuDrawn;
        //Gamestates
        //Track status for easy UI and function switching
        public enum EStatus { Start, Menu, Play, Error, GameOver };
        public static EStatus currStatus;
        //Track current menu
        public enum EMenu { Main, Pause, Submenu };
        public static EMenu currMenu = EMenu.Main;
        #endregion

        #region Initialization
        /// <summary>
        /// Main method called at start
        /// </summary>
        /// <param name="args">Used when started with special arguments</param>
        static void Main(string[] args)
        {
            //Declare this program
            Program p = new Program();

            //Call initialization method and start game loop
            p.Initialize();
            p.GameLoop();
        }
        
        /// <summary>
        /// Preforms main initialization
        /// </summary>
        private void Initialize()
        {
            //Set title
            Console.Title = consoleTitle;

            //Set buffer and window width and height
            Console.BufferWidth = width;
            Console.BufferHeight = height;
            Console.WindowWidth = width;
            Console.WindowHeight = height;

            //Set colors
            Console.ForegroundColor = Printer.defaultTextColor;

            //Set other settings
            Console.CursorVisible = false;

            //Start the game
            currStatus = EStatus.Start;
        }
        #endregion

        #region Game Loop Methods
        /// <summary>
        /// Main loop to run for entire game; handles gamestate switching and end of game
        /// </summary>
        private void GameLoop()
        {
            while (running)
            {
                //Switches the running method to whatever is currently selected
                switch (currStatus)
                {
                    case EStatus.Start:
                        Start();
                        break;
                    case EStatus.Menu:
                        Menu();
                        break;
                    case EStatus.Play:
                        Play();
                        break;
                    //Only used for unknown errors
                    case EStatus.Error:
                        Error();
                        break;
                    case EStatus.GameOver:
                        GameOver();
                        break;
                }
            }
        }

        /// <summary>
        /// Finishes initialization and loads the main menu
        /// </summary>
        private void Start()
        {
            //Get ready to load the main menu
            currMenu = EMenu.Main;
            //When finished, go to menu
            mainMenuDrawn = false;
            currStatus = EStatus.Menu;
        }

        /// <summary>
        /// Loads and handles various menus
        /// </summary>
        private void Menu()
        {
            //If entering the main menu, draw it and prepare to navigate
            if (currMenu == EMenu.Main)
            {
                //Has the user selected an option?
                bool selected = false;

                //Draw the main menu if it is not already drawn
                if (!mainMenuDrawn)
                {
                    Menus.drawMainMenu();
                    mainMenuDrawn = true;
                }

                //Wait for key input, then navigate the menu
                while (!selected)
                {
                    ConsoleKeyInfo pressedKey = Console.ReadKey();

                    if (pressedKey.Key == ConsoleKey.DownArrow)
                    {
                        Menus.navigateDown(Menus.getMainMenuOptions());
                    }

                    if (pressedKey.Key == ConsoleKey.UpArrow)
                    {
                        Menus.navigateUp(Menus.getMainMenuOptions());
                    }

                    if (pressedKey.Key == ConsoleKey.Enter)
                    {
                        Menus.startOption(Menus.getMainMenuOptions());
                        //Stop checking for selection
                        selected = true;
                        //Set to draw main menu later
                        mainMenuDrawn = false;
                    }
                }
            }

            //If entering submenu, set up to return to main menu
            if (currMenu == EMenu.Submenu)
            {
                Menus.setReturn();
            }
        }

        /// <summary>
        /// Preforms most actions during actual gameplay
        /// </summary>
        private void Play()
        {
            //Add play code here
        }

        /// <summary>
        /// Handles an unspecified error
        /// </summary>
        private void Error()
        {
            ErrorHandler.processError();
        }

        /// <summary>
        /// Handles a coded (known) error, such as saving/loading problems
        /// </summary>
        /// <param name="errorCode">Code for error</param>
        private void Error(string errorCode)
        {
            //Add specified error code here
        }

        /// <summary>
        /// Ends the game
        /// </summary>
        private void GameOver()
        {
            //Shut down the game loop
            running = false;
        }
        #endregion
    }
}